#include "flashview.h"
#include <QFileDialog>

FlashView::FlashView()
{

}

void FlashView::init(QWidget *qWidge,Ui::MainWindow *ui,GLParse *glParse)
{
    this->ui = ui;
    this->glParse = glParse;
    this->qWidge = qWidge;

    connect(ui->pushButtonOpenFlashFile,SIGNAL(clicked()),this,SLOT(on_pushButtonOpenFlashFile_clicked()));
    connect(ui->pushButtonFlashStart,SIGNAL(clicked()),this,SLOT(on_pushButtonFlashStart_clicked()));
    connect(&flashThread,SIGNAL(flashStatusSignals(int,QString)),this,SLOT(flashStatusSlots(int,QString)));

    QString binFilePath = settings->value("bin_file_path").toString();
    if(!binFilePath.isEmpty())
    {
        ui->lineEditFlashFilePath->setText(binFilePath);
    }

    flashProgressBar = new QProgressBar();
    flashProgressBar->setRange(0,100);
    flashProgressBar->setValue(0);
    ui->statusBar->addPermanentWidget(flashProgressBar);

}

void FlashView::on_pushButtonOpenFlashFile_clicked()
{
    QString path = settings->value("bin_file_path").toString();

    QString strFile = QFileDialog::getOpenFileName(
                qWidge,
                tr("打开文件"),
                path);

    if(!strFile.isEmpty())
    {
        settings->setValue("bin_file_path",strFile);
        ui->lineEditFlashFilePath->setText(strFile);
    }
}

void FlashView::on_pushButtonFlashStart_clicked()
{
    QString binFilaPath = ui->lineEditFlashFilePath->text();
    if(!binFilaPath.isEmpty())
    {
        ui->pushButtonFlashStart->setEnabled(false);
        flashThread.init(glParse);
        flashThread.setBinPath(binFilaPath);
        flashThread.start();
    }else{
        mlog.show("请加载固件地址");
    }

}

void FlashView::flashStatusSlots(int staus,QString str)
{
    if(staus >= 0)
    {
        flashProgressBar->setValue(staus);
    }else if(staus == -2)
    {
        ui->pushButtonFlashStart->setEnabled(true);
    }

    mlog.show(str);
}
